<?php
/**
 * @author guoxiaoyong
 * 索真接口
 */
namespace api\controllers;

use bc\controller\ApiController;
use bc\util\Util;
use bc\api\SzApi;
use bc\model\ReportModel;
use bc\model\BindModel;
use bc\model\OrderbindMode;
use bc\util\Sms;
use bc\model\api\ApiLogModel;
use bc\service\gene\FpService;
use bc\model\product\QrcodeModel;
use bc\util\ImageMergeQrcode;
use bc\model\order\report\ShareModel;

class SzController extends ApiController
{
	
	protected function verbs()
	{
		return [
				'reveive' => ['POST'],
				'report' => ['POST'],
		];
	}
	
	/**
	 * 收样后回调通知
	 */
	public function actionReceive()
	{
		$res = [];
		
		$getParams['randomStr'] = $this->get('randomStr');
		$getParams['sign'] = $this->get('sign');
		$params = $this->getRequest()->getBodyParams();
		
		$api = new SzApi();
		if($this->diff($getParams, $api->getSign($getParams['randomStr'])) == false)
		{
			$res['retCode'] = 1;
			$res['msg'] = 'invalid argument';
		}
		else
		{
			if(!isset($params['sampleNo']))
			{
				$res['retCode'] = 1;
				$res['msg'] = 'sampleNo not empty';
			}
			else
			{
				
				$OrderBindModel = new OrderbindMode();
				$info = $OrderBindModel->getBindInfo(['bind_no'=>$params['sampleNo']]);
				
				if( $info[0]['bind_status'] < 3 )
				{
					
					$res['retCode'] = -1;
					$res['msg'] = '样本还没绑定';
				}
				else if($info[0]['bind_status'] == 3)
				{
					$OrderBindModel->modify(array('bind_status'=>4),$info[0]['bind_id']);
					$res['retCode'] = 0;
					$res['msg'] = 'success';
				}else if($info[0]['bind_status'] > 3)
				{
					$res['retCode'] = -2;
					$res['msg'] = '该样本已检测';
				}
			}
		}
		
		$data['api_type'] = 3;
		$data['request_params'] = json_encode($params);
		$data['status'] = 1;
		$data['result_params'] = json_encode($res);
		$apiModel = new ApiLogModel();
		$apiModel->add($data);
		
		echo Util::jsonEncode($res);
	}
	
	/**
	 * 报告生成后回调通知
	 */
	public function actionReport()
	{
		$res = [];
		
		$getParams['randomStr'] = $this->get('randomStr');
		$getParams['sign'] = $this->get('sign');
		$params = $this->getRequest()->getBodyParams();
		
		$api = new SzApi();
		if($this->diff($getParams, $api->getSign($getParams['randomStr'])) == false)
		{
			$res['retCode'] = 1;
			$res['msg'] = 'invalid argument';
		}
		else 
		{
			
			if(!isset($params['sampleNo']))
			{
				$res['retCode'] = 1;
				$res['msg'] = 'sampleNo not empty';
			}
			else if(!isset($params['reportData']) || count($params['reportData']) != 5)
			{
				$res['retCode'] = 1;
				$res['msg'] = 'reportData not empty';
			}
			else
			{
				$res['retCode'] = 0;
				$res['msg'] = 'success';
				
				$result = $this->insertData($params['sampleNo'], $params['reportData']);
				if($result == true)
				{
					//生成二维码分享图
					
					$bindModel = new BindModel();
					$bind = $bindModel->getRow(['bind_no' => $params['sampleNo']]);
					if($bind)
					{
						$service = new FpService();
						$backgroundImage = $service->getReportTemplate($bind['bind_no']);
							
						$qrcodeModel = new QrcodeModel();
						$qrcode = $qrcodeModel->getRow(['product_id' => 2, 'user_id' => $bind['user_id'], 'is_delete' => 0]);
							
						$newImage = 'qrcode/product/' . time() . '.jpg';
						//合并图片
						$imageMergeQrcode = new ImageMergeQrcode(STATIC_IMAGE . $backgroundImage, STATIC_IMAGE . $qrcode['qrcode_image']);
						$imageMergeQrcode->setX(282);
						$imageMergeQrcode->setY(1043);
						$imageMergeQrcode->create(UPLOAD_PATH . $newImage);
						
						$shareModel = new ShareModel();
						
						$shareData['share_image'] = $newImage;
						$shareData['bind_no'] = $params['sampleNo'];
						$shareData['is_del'] = 0;
						$shareData['create_time'] = $shareData['update_time'] = date('Y-m-d H:i:s');
						$shareModel->add($shareData);

						$res['retCode'] = 0;
						$res['msg'] = 'success';
					}
					else 
					{

						$res['retCode'] = 1;
						$res['msg'] = 'sampleNo not exists';
					}
					
					
				}
				else 
				{
					$res['retCode'] = 1;
					$res['msg'] = 'push data failed';
				}
			}
		}
		
		
		$data['api_type'] = 4;
		$data['request_params'] = json_encode($params);
		$data['status'] = 1;
		$data['result_params'] = json_encode($res);
		$apiModel = new ApiLogModel();
		$apiModel->add($data);
		
		echo Util::jsonEncode($res);
	}
	
	/**
	 * 比较
	 * @return boolean
	 */
	protected function diff($request, $response)
	{
		if($request['sign'] == $response['sign'] && $request['randomStr'] == $response['random_str'])
			return true;
		else 
			return false;
	}
	
	
	/**
	 * 插入用户数据
	 * 
	 * report_1_value  运动减肥效果肥胖风险
	 * report_2_value  摄食量肥胖风险
	 * report_3_value  偏好油腻食物肥胖风险
	 * report_4_value  脂肪分解能力肥胖风险
	 * report_5_value  糖类脂肪代谢肥胖风险
	 * 
	 * @return boolean
	 */
	protected function insertData($sampleNo, $reportData)
	{
		$reportModel = new ReportModel();
		$bindModel = new BindModel();
		$row = $bindModel->getRow(['bind_no' => $sampleNo]);
		
		$reportInfo = new \stdClass();
		$reportInfo->user_id = $row['user_id'];
		$reportInfo->bind_no = $row['bind_no'];
		$reportInfo->product_id = 2;
		
		$reportModel->tractionInsert($reportData, $reportInfo);
		$bind = $bindModel->getRow(['bind_no' => $sampleNo]);
		if($bind)
		{
			$bindModel->modify(['bind_status' => 5], $bind['bind_id']);
		}
		
		return true;
	}
	
}

